import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class TCPEchoServer {
	public final static int defaulPort = 2014;
	public static void main(String[] args) {
		try{
			ServerSocket ss = new ServerSocket(defaulPort);
			System.out.println("Server dang lang nghe tai cong: "+defaulPort);
			while (true){
				try{
					Socket s = ss.accept();
					OutputStream os = s.getOutputStream();
					InputStream is = s.getInputStream();
					int ch=0;
					while(true){
						ch = is.read();
						System.out.print(is);
						if(ch == -1)break;
						os.write(ch);
					}
					s.close();
				}
				catch(IOException e){
					System.out.println("Connect Error: "+e);
				}
			}
		}
			catch(IOException e){
				System.err.println("Server creation Error: "+e);
			}
		}

	}

